package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.data.DiaDiemObject;
import com.lv.phuotbacnam.bl.log.Logger;
import com.lv.phuotbacnam.bl.queries.BaiDangQueries;
import com.lv.phuotbacnam.bl.queries.DiaDiemQueries;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.lv.phuotbacnam.ui.adapter.HorizontalListViewAdapter;
import com.lv.phuotbacnam.ui.adapter.HorizontalListViewAdapter2;
import com.lv.phuotbacnam.ui.adapter.TListViewApdapter;
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.sileria.android.view.HorzListView;


public class ChitietDiadiem2 extends Activity  {
	ParseObject object_diadiem =  (ParseObject) IntentHelper.getObjectForKey("diadiem");
	ArrayList<MyItem> arrBaidang1 = new ArrayList<MyItem>();
	ArrayList<MyItem_Baidang> arrBaidang = new ArrayList<MyItem_Baidang>();
	ParseObject diadiem_present = null;
	TextView tv_motadiadiem = null;
	ArrayList<MyItem> arrImage = new ArrayList<MyItem>();
	ArrayList<String> arrUrlImage = new ArrayList<String>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.chitietdiadiem);
		//Intent callerIntent=getIntent();
		//Bundle packageFromCaller=
				 //callerIntent.getBundleExtra("MyPackage");		
		//final String tendiadiem = packageFromCaller.getString("tendiadiem");
		final String tendiadiem = object_diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem);
		TextView tv = (TextView)findViewById(R.id.tv_tendiadiem);
		
		tv_motadiadiem = (TextView)findViewById(R.id.tv_motadiadiem);
		
		tv.setText(tendiadiem);
		tv_motadiadiem.setText(object_diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_mo_ta));
		
					HorzListView listviewImg = (HorzListView) findViewById(R.id.lv_imagediadiem);	
					HorizontalListViewAdapter2 adapterImg = new HorizontalListViewAdapter2(object_diadiem,ChitietDiadiem2.this );
					listviewImg.setAdapter(adapterImg);
					
					 for(int i =0; i<arrImage.size();i++)
					 {
						 arrUrlImage.add(arrImage.get(i).getImage());
						 Logger.logMessage("add : " + arrImage.get(i).getImage());
						// arrUrlImage.set(i, arrImage.get(i).getImage());
					 }
					
					 //Logger.logMessage("kich thuoc mang la: " + arrUrlImage.size());
					
					//xử lý sự kiện onclick vào list_Image sẽ mở ra image full sceen
					listviewImg.setOnItemClickListener(new AdapterView.OnItemClickListener() 
					{

						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							 //arrUrlImage.size() = arrImage.size();
							 
							 Intent intent = new Intent(ChitietDiadiem2.this, FullImage.class);
							 IntentHelper.addObjectForKey(object_diadiem,"diadiem");
							 Bundle bundle = new Bundle();
							 bundle.putInt("position", arg2);
							 //bundle.putStringArrayList("arrayUrlImage", arrUrlImage);
							 intent.putExtra("MyPackage", bundle);
							 //Logger.logMessage("click tai vi tri: " + arg2 + " va url: " + arrUrlImage.get(arg2));
							 startActivity(intent);
							
						}
						
					});
			
		
		//String diadiemhientai = diadiem_present.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem);
		//Log.e("ten dia diem ", diadiemhientai);
					ParseQuery<ParseObject> query = ParseQuery
					.getQuery(QueriesConstant.TableBaiDang.Table_Name);
				query.whereEqualTo(QueriesConstant.TableBaiDang.COLUMN_dia_diem,object_diadiem);
				query.findInBackground(new FindCallback<ParseObject>() {

					@Override
					public void done(List<ParseObject> baidangs,
							ParseException e) {
						if (e == null && baidangs.size() > 0) {
							for(ParseObject baidang: baidangs )
							{
								

								//String chaytoi	=	diadiem_present.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem);					  
								
								
								ParseObject object = null;
								object =baidang.getParseObject(QueriesConstant.TableBaiDang.COLUMN_dia_diem);
								//String a = baidang.getString(QueriesConstant.TableBaiDang.COLUMN_cam_nhan);
								//Log.e("cam nhan ", a);
								
								
								
								//String text = object.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem);
								
									//if(diadiem_present == object)
									//{
										JSONArray list_photo_baidang = baidang.getJSONArray(QueriesConstant.TableBaiDang.COLUMN_list_photo);
										JSONObject first_photo;
										try {
											first_photo = list_photo_baidang.getJSONObject(0);
											String urlFirstimage = first_photo.getString("url");
											String aTenbaidang = baidang.getString(QueriesConstant.TableBaiDang.COLUMN_ten_bai_dang);
											String aCamnhan = baidang.getString(QueriesConstant.TableBaiDang.COLUMN_cam_nhan);
											MyItem_Baidang new_baidang = new MyItem_Baidang(urlFirstimage,aTenbaidang,aCamnhan);
											MyItem newItem = new MyItem(aTenbaidang, urlFirstimage);
											arrBaidang1.add(newItem);
											arrBaidang.add(new_baidang);
										} catch (JSONException e1) {
											// TODO Auto-generated catch block
											e1.printStackTrace();
										}
										
									//}

							}
							Log.e("size of array ", arrBaidang.size()+"");
							for(int i = 0;i<arrBaidang.size();i++)
							{
								Log.e("ten bai dang ", arrBaidang.get(i).getTenbaidang());
								Log.e("url image ", arrBaidang.get(i).getUrlImage());
								Log.e("cam nhan ", arrBaidang.get(i).getCamnhan());
							}
							ListView lv_baidangcuadiadiem = (ListView)findViewById(R.id.lv_baidangcuadiadiem);
							//HorzListView lv_baidangcuadiadiem = (HorzListView)findViewById(R.id.lv_baidangcuadiadiem);
							TListViewApdapter adapter = new TListViewApdapter(arrBaidang, ChitietDiadiem2.this);
							lv_baidangcuadiadiem.setAdapter(adapter);
							lv_baidangcuadiadiem.setOnItemClickListener(new OnItemClickListener() {

								@Override
								public void onItemClick(AdapterView<?> parent,
										View view, int position, long id) {
									//Intent intent = new Intent(rootView.getContext(), ChitietDiadiem.class);
									//IntentHelper.addObjectForKey(arrParseObject_baidang.get(arg2),"baidang");
									//startActivity(intent);

									
								}
							
							});
							//HorizontalListViewAdapter adapterImg = new HorizontalListViewAdapter(arrBaidang1, ChitietDiadiem2.this);
							//lv_baidangcuadiadiem.setAdapter(adapterImg);
						
						}

					}
				});
		
		
		
		
	}	
}
